package service;

import BaseQuery.BaseQuery;
import Bean.PageBean;
import Jdbc.Employee;
import dao.EmployeeDao1;

import java.sql.SQLException;
import java.util.List;

public class EmployeeService {
	private EmployeeDao1 employeeDao1 = null;

	public EmployeeService() {
		employeeDao1 = new EmployeeDao1();
	}

	public boolean add(Employee emp) throws SQLException {
		if (emp == null) {
			return false;
		}
		if(emp.getEname()==null||"".equals(emp.getEname())) {
			return false;
		}
		int count = employeeDao1.add(emp);
		return count > 0;
	}

	public boolean update(Employee emp) throws SQLException {
		if(emp==null) {
			return false;
		}
		if(emp.getEmpno()==null||emp.getEname()==null||"".equals(emp.getEname())) {
			return false;
		}
		int count=employeeDao1.update(emp);
		return count>0;
	}
	public Employee getById(Integer empno) throws Exception {
		if(empno==null) {
			return null;
		}
		 Employee employee=employeeDao1.getById(empno);
		return employee;
	}
	public PageBean<Employee> getByPage( BaseQuery baseQuery) throws Exception {
		//当为空值的时候，按默认的进行查询
		if(baseQuery==null) {
			baseQuery=new BaseQuery();
		}
		//计算总条数
		Integer totalRows =employeeDao1.getCount();
		Integer totalPages=null;
		//计算页数
		if(totalRows% baseQuery.getPageCount()==0) {
			totalPages=totalRows/ baseQuery.getPageCount();
		} else {
			totalPages=totalRows/ baseQuery.getPageCount()+1;
		}

		List<Employee> empList = employeeDao1.getByPage(baseQuery);
		//通过创建一个对象，来将数据全部打包给返回去
		PageBean<Employee> pageBean = new PageBean<>();
		pageBean.setTotalRows(totalRows);
		pageBean.setTotalPages(totalPages);
		pageBean.setData(empList);

		return pageBean;

	}
}


